package com.vim.common.utils;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * 随机字符串工具类
 */
public class IdGen {

    /**
     * 32为MD5值
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 32为MD5值
     */
    public static String token() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 随机code
     */
    public static String code(){
        Integer code = (int)((Math.random()*9+1)*100000);
        return code.toString();
    }

    /**
     * 日期格式目录
     */
    public static String dateDirectory(){
        Calendar date=Calendar.getInstance();
        SimpleDateFormat format1=new SimpleDateFormat( "yyyy");
        SimpleDateFormat format2=new SimpleDateFormat( "MM");
        SimpleDateFormat format3=new SimpleDateFormat( "dd");
        String name1=format1.format(date.getTime());
        String name2=format2.format(date.getTime());
        String name3=format3.format(date.getTime());
        return name1 + "/" + name2 + "/" + name3 + "/";
    }

    /**
     * 密码加密
     * @param username 盐
     * @param password 密码
     */
    public static String password(String username, String password){
        String hashAlgorithName = "MD5";
        int hashIterations = 1024;//加密次数
        ByteSource credentialsSalt = ByteSource.Util.bytes(username);
        Object obj = new SimpleHash(hashAlgorithName, password, credentialsSalt, hashIterations);
        return obj.toString();
    }
}
